import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import QtreeWidgetUseing

class TreeEvent(QWidget):
    def __init__(self, parent=None):
        super(TreeEvent, self).__init__(parent)

        self.setWindowTitle("位数节点添加响应控件")
        self.tree = QTreeWidget()
        # self.ui = QtreeWidgetUseing.Ui_Dialog()
        self.tree.setColumnCount(2)
        self.tree.setHeaderLabels(['Key', 'value'])

        addBtn = QPushButton('create note')
        deleteBtn = QPushButton('delete note')
        updateBtn = QPushButton('update note')

        operratorLayout = QHBoxLayout()
        operratorLayout.addWidget(addBtn)
        operratorLayout.addWidget(updateBtn)
        operratorLayout.addWidget(deleteBtn)


        self.tree = QTreeWidget()

        self.tree.setColumnCount(2)
        self.tree.setHeaderLabels(['Key', 'value'])

        root = QTreeWidgetItem(self.tree)
        root.setText(0, 'root')
        root.setText(1, '1')

        # flies = QTreeWidgetItem(self.tree)
        # flies.setText(0, 'flies')
        # flies.setText(1, 'open')

        child1 = QTreeWidgetItem(root)
        child1.setText(0, 'child1')
        child1.setText(1, '1')

        self.tree.clicked.connect(self.onTreeClicked)
        addBtn.clicked.connect(self.addNote)
        deleteBtn.clicked.connect(self.deleteNote)
        updateBtn.clicked.connect(self.updataNote)

        mainlayout = QVBoxLayout(self)
        mainlayout.addLayout(operratorLayout)
        mainlayout.addWidget(self.tree)
        self.setLayout(mainlayout)

    def onTreeClicked(self):
        item = self.tree.currentItem()
        print('key = %s, and Value = %s' % (item.text(0), item.text(1)))

    def addNote(self):
        item = self.tree.currentItem()
        print(item)
        node = QTreeWidgetItem(item)
        node.setText(0, 'new Note')
        node.setText(1, "new Vale")

    def deleteNote(self):
        item = self.tree.currentItem()
        root = self.tree.invisibleRootItem()
        for item in self.tree.selectedItems():
            (item.parent() or root).removeChild(item)

    def updataNote(self):
        item = self.tree.currentItem()
        print(item)
        item.setText(0, 'new Note')
        item.setText(1, "new Vale")



if __name__ == '__main__':

    app = QApplication(sys.argv)
    exec_ = TreeEvent()
    exec_.show()
    sys.exit(app.exec_())